<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>15 - OL4加载天地图WMTS服务展示地图</title>
    <script src='../lib/shapefile.js'></script>
    <script src='../lib/shp.js'></script>
    <script src="../lib/ol4/ol4.js"></script>
    <script src="../lib/proj4/proj4.js"></script>
    <link rel="stylesheet" href="../css/ol4/ol.css">
    <link rel="stylesheet" href="../css/main.css">
</head>
<body>
<div id="map"></div>
</body>
<script>
    const points = [
        [114.29, 30.59]
    ];
    const projection = ol.proj.get('EPSG:4326');
    console.log(projection);
    projection.setExtent([-180, -90, 180, 90]);
    const tileSize = 256;
    const projectionExtent = projection.getExtent();
    const origins = [
        [-180.0, 90.0],
        ol.extent.getTopLeft(projectionExtent)
    ];
    const origin = origins[1];
    console.log('extent:', projectionExtent);
    console.log('origin:', origin);

    const size = ol.extent.getWidth(projectionExtent) / tileSize;

    const resolutions = new Array(18);
    const matrixIds = new Array(18);
    for (let z = 1; z < 21; z++) {
        resolutions[z] = size / Math.pow(2, z);
        matrixIds[z] = z;
    }
    console.log(resolutions);

    const tileGrid = new ol.tilegrid.WMTS({
        origin,
        resolutions: resolutions,
        extent: projectionExtent,
        matrixIds: matrixIds
    });

    const url = 'http://t0.tianditu.gov.cn/img_c/wmts?tk=4b847b6d842e806adc1bab6696a4f4ac';
    const url1 = 'http://t0.tianditu.gov.cn/cia_c/wmts?tk=4b847b6d842e806adc1bab6696a4f4ac';

    // 底图瓦片
    const wmtsSource = new ol.source.WMTS({
        url,
        layer: 'img',
        matrixSet: 'c',
        format: 'tiles',
        projection: projection,
        tileGrid: tileGrid,
        style: 'default',
        wrapX: false
    });
    // 图层
    const baseLayer = new ol.layer.Tile({
        source: wmtsSource
    });

    // 图层标注
    const wmtsSource1 = new ol.source.WMTS({
        url: url1,
        layer: 'cia',
        matrixSet: 'c',
        format: 'tiles',
        projection: projection,
        tileGrid: tileGrid,
        style: 'default',
        wrapX: false
    });
    // 图层
    const baseLayer1 = new ol.layer.Tile({
        source: wmtsSource1
    });

    const style = new ol.style.Style({
        fill: new ol.style.Fill({
            color: 'rgba(255,0,0,0.0)'
        }),
        stroke: new ol.style.Stroke({
            color: 'rgba(255,255,0,1)',
            width: 4
        })
    });

    const source1 = new ol.source.Vector({
        wrapX: false
    });

    const vector = new ol.layer.Vector({
        source: source1,
        style
    });

    // const circlePoint = ol.proj.transform([116.384722, 39.889916], 'EPSG:4326', 'EPSG:4490');

    const centerPoint = points[0];

    const iconFeature = new ol.Feature({
        geometry: new ol.geom.Point(centerPoint)
    });

    const iconStyle = new ol.style.Style({
        image: new ol.style.Icon({
            anchor: [10, 26],
            anchorXUnits: 'pixels',
            anchorYUnits: 'pixels',
            src: '../image/gis_rydw.gif'
        })
    });

    iconFeature.setStyle(iconStyle);

    const vectorSource = new ol.source.Vector({
        features: [iconFeature],
        wrapX: false
    });

    const iconLayer = new ol.layer.Vector({
        source: vectorSource
    });

    const map = new ol.Map({
        layers: [
            baseLayer,
            baseLayer1,
            vector,
            iconLayer
        ],
        target: 'map',
        view: new ol.View({
            center: centerPoint,
            projection,
            zoom: 10,
            maxZoom: 21,
            minZoom: 1,
            resolutions
        })
    });

    const geojsonformat = new ol.format.GeoJSON();


    shapefile.open('./data/管护区.shp')
        .then(source => {
            source.read().then(function log(result) {
                if (result.done) return;
                const feature = geojsonformat.readFeature(result.value);
                console.log('feature', feature);
                source1.addFeature(feature);
                return source.read().then(log);
            });
        });

    // const metersPerUnit = map.getView().getProjection().getMetersPerUnit();
    //
    // const realRadius = radius / metersPerUnit;
    //
    // const circleFeature = new ol.Feature({
    //     geometry: new ol.geom.Circle(centerPoint, realRadius)
    // });

</script>
</html>